package com.study;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class TestTX {

    public static void main(String[] args) {

        Jedis jedis = new Jedis("47.93.26.49", 6379);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello","world");
        jsonObject.put("test","value");
        String result = jsonObject.toJSONString();

        //开始事务
        Transaction multi = jedis.multi();
        try {
            //事务操作
            multi.set("user1",result);
            multi.set("user2",result);
            //事务执行
            multi.exec();
        } catch (Exception e) {
            //放弃事务
            multi.discard();
            e.printStackTrace();
        } finally {
            System.out.println(jedis.get("user1"));
            //关闭连接
            multi.close();
        }


    }
}
